//
//  PersonnelListViewController.m
//  cityStar
//
//  Created by joyban on 14-12-24.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "PersonnelListViewController.h"
#import "PensonnelListCell.h"

#import "ServiceDetailViewController.h"
#import "WaresDetailViewController.h"
#import "SingleTableView.h"

#import "HWSearchViewController.h"

NSString *const PensonnelListTableViewCellID = @"PensonnelListCellID";


@interface PersonnelListViewController ()<SingleTableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *PersonnelListTableView;
@property (weak, nonatomic) IBOutlet UIButton *btnAlltypes;
@property (weak, nonatomic) IBOutlet UIButton *btnIntelligence;

@property (nonatomic, weak) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *typeImageView;
@property (strong, nonatomic) SingleTableView *typeSingleTableView;

@property (nonatomic, weak) IBOutlet UIImageView *sortImageView;
@property (strong, nonatomic) SingleTableView *sortSingleTableView;

@end

@implementation PersonnelListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"人员列表";
    [self addBackButton];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithImage:[UIImage imageNamed:@"bsearch" ] actionBlock:^{
        HWSearchViewController *hwSearchViewController = [[HWSearchViewController alloc]init];
        hwSearchViewController.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:hwSearchViewController animated:YES];
    }];
    [self setUpTableViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.PersonnelListTableView registerNib:[PensonnelListCell nib] forCellReuseIdentifier:PensonnelListTableViewCellID];
}

- (void)addRightButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 20, 20)];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:@"bsearch_touch"] forState:UIControlStateHighlighted];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [PensonnelListCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PensonnelListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:PensonnelListTableViewCellID];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return ListdetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    ServiceDetailViewController *serviceDetailViewController = [[ServiceDetailViewController alloc] init];
    [self.navigationController pushViewController:serviceDetailViewController animated:true];
}

#pragma mark - Actions
-(IBAction)allTypes_atction:(id)sender
{
    if(self.btnAlltypes.selected == NO)
    {
        self.btnAlltypes.selected = YES;
    }else
    {
        self.btnAlltypes.selected = NO;
    }
    self.btnIntelligence.selected = NO;
    
    [self.typeSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
    self.typeSingleTableView.dataArray = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"];
}

-(IBAction)Intelligence_atction:(id)sender
{
    if(self.btnIntelligence.selected == NO)
    {
        self.btnIntelligence.selected = YES;
    }else
    {
        self.btnIntelligence.selected = NO;
    }
    self.btnAlltypes.selected = NO;
    
    [self.sortSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
    self.sortSingleTableView.dataArray = @[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"];
}

#pragma mark - singleTableViewDelegate
- (SingleTableView *)typeSingleTableView
{
    if (!_typeSingleTableView) {
        _typeSingleTableView = [[SingleTableView alloc] init];
        _typeSingleTableView.delegate = self;
        [self addChildViewController:_typeSingleTableView];
        [self.view addSubview:_typeSingleTableView.view];
        [self.view bringSubviewToFront:self.topView];
        _typeSingleTableView.arrawImageView = self.typeImageView;
    }
    return _typeSingleTableView;
}

- (void)singleTableView:(SingleTableView *)tableView didSelectedCellAtIndexPath:(NSIndexPath *)indexPath
{
    self.btnAlltypes.selected = NO;
    self.btnIntelligence.selected = NO;
    
    if (tableView == self.typeSingleTableView) {
        NSString *celltest =@[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"][indexPath.row];
        if (celltest.length > 5) {
            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 5)]];
            [self.btnAlltypes setTitle:btnTitle forState:UIControlStateNormal];
        }else{
            [self.btnAlltypes setTitle:celltest forState:UIControlStateNormal];
            
        }
    }else
    {
        NSString *celltest =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"][indexPath.row];
        if (celltest.length > 5) {
            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 5)]];
            [self.btnIntelligence setTitle:btnTitle forState:UIControlStateNormal];
        }else{
            [self.btnIntelligence setTitle:celltest forState:UIControlStateNormal];
            
        }
    }
    
}

-(void)cancleTabelView
{
    self.btnAlltypes.selected = NO;
    self.btnIntelligence.selected = NO;
}

- (SingleTableView *)sortSingleTableView
{
    if (!_sortSingleTableView) {
        _sortSingleTableView = [[SingleTableView alloc] init];
        _sortSingleTableView.delegate = self;
        [self addChildViewController:_sortSingleTableView];
        [self.view addSubview:_sortSingleTableView.view];
        [self.view bringSubviewToFront:self.topView];
        _sortSingleTableView.arrawImageView = self.sortImageView;
    }
    return _sortSingleTableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
